package com.myweb;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

public class WebRequest {

  private HttpServletRequest httpRequest;

  public WebRequest(HttpServletRequest httpRequest) {
    this.httpRequest = httpRequest;
  }

  public String getString(String key) {
    return httpRequest.getParameter(key);
  }

  public Integer getInteger(String key) throws WebException {
    String param = httpRequest.getParameter(key);
    if (param == null) {
      return null;
    } else {
      try {
        return Integer.parseInt(param);
      } catch (NumberFormatException e) {
        throw new WebException(key + " should be int");
      }
    }
  }

  public Float getFloat(String key) throws WebException {
    String param = httpRequest.getParameter(key);
    if (param == null) {
      return null;
    } else {
      try {
        return Float.parseFloat(param);
      } catch (NumberFormatException e) {
        throw new WebException(key + " should be float");
      }
    }
  }

  public Boolean getBoolean(String key) {
    String param = httpRequest.getParameter(key);
    if (param == null) {
      return null;
    } else {
      return Boolean.parseBoolean(param);
    }
  }

  public String getStringNN(String key) throws WebException {
    String param = httpRequest.getParameter(key);
    if (param == null) {
      throw new WebException(key + " should not be null");
    } else {
      return param;
    }
  }

  public Integer getIntegerNN(String key) throws WebException {
    String param = httpRequest.getParameter(key);
    if (param == null) {
      throw new WebException(key + " should not be null");
    } else {
      try {
        return Integer.parseInt(param);
      } catch (NumberFormatException e) {
        throw new WebException(key + " should be int");
      }
    }
  }

  public Float getFloatNN(String key) throws WebException {
    String param = httpRequest.getParameter(key);
    if (param == null) {
      throw new WebException(key + " should not be null");
    } else {
      try {
        return Float.parseFloat(param);
      } catch (NumberFormatException e) {
        throw new WebException(key + " should be float");
      }
    }
  }

  public Boolean getBooleanNN(String key) throws WebException {
    String param = httpRequest.getParameter(key);
    if (param == null) {
      throw new WebException(key + " should not be null");
    } else {
      return Boolean.parseBoolean(param);
    }
  }

  public List<String> getStringList(String key) {
    String[] param = httpRequest.getParameterValues(key);
    if (param == null) {
      return null;
    } else {
      return Arrays.asList(param);
    }
  }

  public List<Integer> getIntegerList(String key) throws WebException {
    String[] param = httpRequest.getParameterValues(key);
    List<Integer> list;
    if (param == null) {
      return null;
    } else {
      list = new ArrayList<>();
      for (int i = 0; i < param.length; i++) {
        try {
          list.add(Integer.parseInt(param[i]));
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be int");
        }
      }
      return list;
    }
  }

  public List<Float> getFloatList(String key) throws WebException {
    String[] param = httpRequest.getParameterValues(key);
    List<Float> list;
    if (param == null) {
      return null;
    } else {
      list = new ArrayList<>();
      for (int i = 0; i < param.length; i++) {
        try {
          list.add(Float.parseFloat(param[i]));
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be int");
        }
      }
      return list;
    }
  }

  public List<Boolean> getBooleanList(String key) {
    String[] param = httpRequest.getParameterValues(key);
    List<Boolean> list;
    if (param == null) {
      return null;
    } else {
      list = new ArrayList<>();
      for (int i = 0; i < param.length; i++) {
        list.add(Boolean.parseBoolean(param[i]));
      }
      return list;
    }
  }

  public List<String> getStringListNN(String key) throws WebException {
    String[] param = httpRequest.getParameterValues(key);
    if (param == null) {
      throw new WebException(key + " should not be null");
    } else {
      return Arrays.asList(param);
    }
  }

  public List<Integer> getIntegerListNN(String key) throws WebException {
    String[] param = httpRequest.getParameterValues(key);
    List<Integer> list;
    if (param == null) {
      throw new WebException(key + " should not be null");
    } else {
      list = new ArrayList<>();
      for (int i = 0; i < param.length; i++) {
        try {
          list.add(Integer.parseInt(param[i]));
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be int");
        }
      }
      return list;
    }
  }

  public List<Float> getFloatListNN(String key) throws WebException {
    String[] param = httpRequest.getParameterValues(key);
    List<Float> list;
    if (param == null) {
      throw new WebException(key + " should not be null");
    } else {
      list = new ArrayList<>();
      for (int i = 0; i < param.length; i++) {
        try {
          list.add(Float.parseFloat(param[i]));
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be int");
        }
      }
      return list;
    }
  }

  public List<Boolean> getBooleanListNN(String key) throws WebException {
    String[] param = httpRequest.getParameterValues(key);
    List<Boolean> list;
    if (param == null) {
      throw new WebException(key + " should not be null");
    } else {
      list = new ArrayList<>();
      for (int i = 0; i < param.length; i++) {
        list.add(Boolean.parseBoolean(param[i]));
      }
      return list;
    }
  }
}
